<?php

/**
 * UserModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 3 2012-07-25 15:21:19Z rendong237@gmail.com $
 */
class UserModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["uid"] = (int)$value["uid"];
		$v["email"] = strtolower($value["email"]);
		$v["password"] = $value["password"];
		//$v["enfname"] = strtoupper($value["enfname"]);
		//$v["enlname"] = strtoupper($value["enlname"]);
		$v["enfname"] = $value["enfname"];
		$v["enlname"] = $value["enlname"];
		$v["name"] = $value["name"];
		$v["gender"] = (int)$value["gender"];
		$v["nation"] = $value["nation"];
		$v["birth"] = $value["birth"];
		$v["idcode"] = $value["idcode"];
		$v["unit"] = $value["unit"];
		
		if ($value["cnunit"] != 'undefined')
			$v["cnunit"] = $value["cnunit"];
		
		$v["phone"] = $value["phone"];
		$v["fax"] = $value["fax"];
		$v["address"] = $value["address"];
		$v["pcode"] = $value["pcode"];
		$v["regip"] = $value["regip"];
		$v["regtime"] = $value["regtime"];
		$v["headphoto"] = $value["headphoto"];
		$v["idcard"] = $value["idcard"];
		$v["idphoto"] = $value["idphoto"];
		
		return $this->db->insert("user", $v, true);
	}
	
	/**
	 * 修改
	 */
	public function change($uid, array $value)
	{
		$v = array();
		isset($value["email"]) && $v["email"] = $value["email"];
		isset($value["password"]) && $v["password"] = $value["password"];
		isset($value["enfname"]) && $v["enfname"] = $value["enfname"];
		isset($value["enlname"]) && $v["enlname"] = $value["enlname"];
		isset($value["name"]) && $v["name"] = $value["name"];
		isset($value["gender"]) && $v["gender"] = (int)$value["gender"];
		isset($value["nation"]) && $v["nation"] = $value["nation"];
		isset($value["birth"]) && $v["birth"] = $value["birth"];
		isset($value["idcode"]) && $v["idcode"] = $value["idcode"];
		isset($value["unit"]) && $v["unit"] = $value["unit"];
		
		if ($value["cnunit"] != 'undefined')
			isset($value["cnunit"]) && $v["cnunit"] = $value["cnunit"];
		
		isset($value["phone"]) && $v["phone"] = $value["phone"];
		isset($value["fax"]) && $v["fax"] = $value["fax"];
		isset($value["address"]) && $v["address"] = $value["address"];
		isset($value["pcode"]) && $v["pcode"] = $value["pcode"];
		isset($value["regip"]) && $v["regip"] = (int)$value["regip"];
		isset($value["regtime"]) && $v["regtime"] = $value["regtime"];
		isset($value["headphoto"]) && $v["headphoto"] = $value["headphoto"];
		isset($value["idcard"]) && $v["idcard"] = $value["idcard"];
		isset($value["idphoto"]) && $v["idphoto"] = $value["idphoto"];

		$this->db->update("user", $v, 'uid=' . (int)$uid);
	}
	
	/**
	 * 删除
	 */
	public function del($uid)
	{
		$this->db->delete("user", 'uid=' .  (int)$uid);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page)
	{
		$where = '';		
		$totalNum = $this->db->fetchOne ('select count(*) from user ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from user' . $where .' order by uid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	public function fetchByEmail($email, $nowp, $per_page)
	{
		$where = ' WHERE email like "%' . $email . '%"';
		$totalNum = $this->db->fetchOne ('select count(*) from user ' . $where);
	
		require_once APP . '/application/models/Util.php';
	
		$pageInfo = Util::page ($totalNum, $per_page, $nowp);
		$start    = $pageInfo ['start'];
		$list     = $this->db->fetchAll ('select * from user' . $where .' order by uid desc limit ' . $start . ',' . $per_page);
	
		return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 显示
	 */
	public function show($uid)
	{
		return $this->db->fetchRow("select * from `user` where uid=\"?\"", array($uid));
	}
	
	/**
	 * 显示
	 */
	public function showByMail($email)
	{
		$email = strtolower($email);
		return $this->db->fetchRow("select * from `user` where email=\"?\"", array($email));
	}
	
	public function isEmailReg($mail)
	{
		return $this->db->fetchOne("select count(*) from user where email='?'", array($mail));
	}
}